<?php

namespace app\common\model;

use app\admin\model\Admin;
use think\Model;


class CommentRule extends Model
{
    // 表名
    protected $name = 'comment_rule';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'time_type_text',
        'behavior_category_type_text',
    ];

    const WEEK = 1;
    const CUSTOMER = 2;
    public static $timeTypeList = [
        '1' => '每周',
        '2' => '自定义',
    ];

    public static $behaviorCategoryTypeList = [
        '1' => '班级',
        '2' => '宿舍',
    ];

    public static $weekList = [
        '1' => '星期一',
        '2' => '星期二',
        '3' => '星期三',
        '4' => '星期四',
        '5' => '星期五',
        '6' => '星期六',
        '7' => '星期日',
    ];

    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("H:i", $value) : $value;
    }

    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("H:i", $value) : $value;
    }

    public function getCustomerStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['customer_start_time']) ? $data['customer_start_time'] : '');
        return is_numeric($value) ? datetime($value) : $value;
    }

    public function getCustomerEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['customer_end_time']) ? $data['customer_end_time'] : '');
        return is_numeric($value) ? datetime($value) : $value;
    }

    public function getTimeTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['time_type']) ? $data['time_type'] : '');
        return isset(self::$timeTypeList[$value]) ? self::$timeTypeList[$value] : '';
    }

    public function getBehaviorCategoryTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['behavior_category_type']) ? $data['behavior_category_type'] : '');
        return isset(self::$behaviorCategoryTypeList[$value]) ? self::$behaviorCategoryTypeList[$value] : 0;
    }

    public function getNoteAttr($value, $data)
    {
        switch ($data['time_type']) {
            case self::WEEK:
                $attr = [];
                foreach (explode(',', $data['week']) as $key => $value) {
                    if (isset(self::$weekList[$value])) {
                        $attr[] = self::$weekList[$value];
                    }
                }
                return implode(',', $attr);
                break;
            case self::CUSTOMER:
                return datetime($data['customer_start_time']) . ' - ' . datetime($data['customer_end_time']);
                break;
        }
    }

    public function setWeekAttr($value)
    {
        if ($value) {
            return implode(',', $value);
        } else {
            return '';
        }
    }

    public function school()
    {
        return $this->belongsTo(Admin::class);
    }

    /**
     * 检测当前时间点是否可以点评
     *
     * @User ZhenZiHan
     * @Date 2020/3/27
     *
     * @param $behavior_category_type 分类
     * @return int|string
     * @throws \think\Exception
     */
    public static function checkRule($behavior_category_type)
    {
        return true;// 暂时不需要此功能 20200401

        $time = datetime(time(), 'H:i:s'); // 当前时间点 10:20:08
        return self::where('behavior_category_type', $behavior_category_type)
            ->where(function ($query) use ($time) {
                $query->where('time_type', self::CUSTOMER)
                    ->where('customer_start_time', '<=', time())
                    ->where('customer_end_time', '>', time())
                    ->where('start_time', '<=', $time)
                    ->where('end_time', '>', $time);
            })
            ->whereOr(function ($query) use ($time) {
                $week = date('w', time());// 今天周几
                $query->where('time_type', self::WEEK)
                    ->where("FIND_IN_SET($week,week)")
                    ->where('start_time', '<=', $time)
                    ->where('end_time', '>', $time);
            })
            ->find();
    }

    /**
     * 检测是否可以点评
     *
     * @User ZhenZiHan
     * @Date 2020/4/1
     *
     * @param $behavior_category_type 分类
     * @param $record_user_id  点评人
     * @return array|bool
     * @throws \think\Exception
     */
    public static function checkBehaviorAdd($behavior_category_type, $record_user_id)
    {
        return true;// 暂时不需要此功能 20200401

        $rule = self::checkRule($behavior_category_type);
        if (!$rule) {
            return [
                'error' => 1,
                'msg' => '当前时间段不允许点评'
            ];
        }

        $record_count = BehaviorRecord::where('record_user_id', $record_user_id)
            ->where(function ($query) use ($behavior_category_type) {
                switch ($behavior_category_type) {
                    case 'classes':
                        $query->where('behavior_category_type', BehaviorCategory::CLASSES);
                        break;
                    case 'dorm':
                        $query->whereIn('behavior_category_type', [BehaviorCategory::DORM, BehaviorCategory::DORM_STUDENT]);
                        break;
                }
            })
            ->whereTime('created_at', 'between', [strtotime($rule->start_time), strtotime($rule->end_time)])
            ->count();

        if ($record_count) {
            return [
                'error' => 1,
                'msg' => '不允许重复点评'
            ];
        }

        return true;
    }


}
